<template>
	<uni-nav-bar :title="name" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	<view class="package" style="padding: 10rpx 20rpx 0px;">
		<uni-segmented-control
		  :current="current"
		  :values="items"
		  @clickItem="onClickItem" 
		  styleType="text"
		  activeColor="#1D92FF">
		</uni-segmented-control>
	</view>
	<!-- 列表 -->
	<scroll-view
	  scroll-y="true"
	  enable-back-to-top
	  @scrolltolower="loadMore"
	  class="scroll-box"
	  :style="{ height: pageHeight + 'px' }"
	>
	<view v-for="(item,index) in orderLists" :key='item.id' 
	class="package"
	@click="navTo('team/groupDetail?id='+item.id+'&team='+GroupID)" style="margin-top: 0px;padding-bottom: 10rpx;">
		<view class="center space-between ">
			<view class="direction-start polp" style="margin-top: 0px;">
				<view>{{item.title}}</view>
				<view class="center" style="width: 100%;">
					<text>创建人</text>
					<image :src="item.fx_avatar" mode=""></image>
					<text>{{item.fx_nickname}}</text>
				</view>
			</view>
			<view class="direction-start" style="align-items: flex-end;" v-if="item.status=='2'">
				<image class="polpImg" :src="Expired" mode="" ></image>
				<text style="color: #666;font-size: 20rpx;">{{item.descript}}</text>
			</view>
		</view>
		<shopList :type="'subsidy'" :shoppAll="item.goods_list"></shopList>
		<view class="Paid center" style="padding-bottom: 0px;">
			<view><text>总价</text></view>
		    <view style="margin: 0px 15rpx;"><text>¥ </text><text>{{item.money}}</text></view>
			<view style="color: #F54B39;font-size: 20rpx;margin-top: 10rpx;" v-if="item.jia_text">{{item.jia_text}}</view>
		</view>
		<TeamPrice :state="'0'" :priceList="item.price_list" :addNums="'0'"></TeamPrice>
		<view class="space-between copys" v-if="item.status=='2'">
			<view></view>
		    <view @click.stop="Onekey(item.bao_id)">复制同款</view>
		</view>
	</view>
	<view class="column fillings" v-if="orderLists.length<1" :style="{ height: pageHeight + 'px' }">
	  <image :src="Empty"></image>
	  <text>暂无团惠包记录</text>
	</view>
	</scroll-view>
	<!-- 新建团惠包弹框 -->
	<uni-popup ref="PobulicePopup" type="center" :is-mask-click="false">
	 <pobulice :title="'温馨提示'" :text="text" @close="closeBankPad" @submit="submit" :type="'center'"></pobulice>
	</uni-popup>
</template>

<script setup lang="ts">
import { navBack, navTo } from '@/utils/navigator';
import { ref } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import pobulice from '@c/pobulic/public.vue';
import TeamPrice from '@c/groupBenefits/teamPrice.vue';
import shopList from '@c/order/shopList.vue';
import { Expired } from '@/utils/Static';
import { toPublish } from '@mqtt';
import { Empty } from '@/utils/Static';
import { errorToast, successToast, showLoading } from '@/utils/prompt';
import { numsTeam, teamGroup } from '@/gql/group';
import { getDriverID } from '@/stores/driverID';
const user_id = getDriverID();
const current = ref(0);
const items = ['全部', '有效', '过期'];
const pageHeight = ref();
const GroupID = ref('');
const repeat = ref(true); //放重复点击

const isLoading = ref(false);
const lastPage = ref(1);
const currentPage = ref(1);
const orderLists = ref([]);

const groupList = ref();
const borId = ref('');
const name = ref('');
const status = ref('9');

const PobulicePopup = ref(null);
const text = '您当前有正在创建新的团惠包  如果继续您新的团惠包内容将被覆盖';
onLoad((pearm) => {
  uni.getSystemInfo({
    success: function(res) {
      pageHeight.value = (res.windowHeight - 105).toString();
    },
  });
  GroupID.value = pearm.id;
  name.value = pearm.name + '团惠包';
  init();
});
onShow(() => {
  cartTeams();
});
function init() {
  isLoading.value = true;
  const payload = {
    query: teamGroup,
    variables: {
      page: currentPage.value,
      user_id,
      team_id: Number(GroupID.value),
      status: status.value,
    },
  };
  toPublish(
    'ql/store/getScoreGoodsList',
    payload,
    (obj: any) => {
      const { team_bao_list } = obj.data;
      isLoading.value = false;
      orderLists.value = [...orderLists.value, ...team_bao_list.list];
      lastPage.value = team_bao_list.lastpage;
    },
    () => {
      errorToast('通讯异常');
      isLoading.value = false;
    },
  );
}
/**
 * 加载更多
 */
function loadMore() {
  if (currentPage.value < lastPage.value) {
    currentPage.value++;
    init();
  }
}
function onClickItem(e) {
  current.value = e.currentIndex;
  status.value = e.currentIndex == '0' ? '9' : e.currentIndex == '1' ? '1' : e.currentIndex == '2' ? '2' : '';
  orderLists.value = [];
  currentPage.value = 1;
  init();
}
/**
 * 获取团惠包数量
 */
function cartTeams() {
  const payload = {
    query: numsTeam,
    variables: {
      user_id,
    },
  };
  toPublish('ql/store/getCartTeamList', payload, (obj: any) => {
    const { team_shopping_nums } = obj.data;
    groupList.value = team_shopping_nums.nums;
  });
}
function Onekey(id) {
  borId.value = id;
  if (groupList.value < 1) {
    addGroup();
  } else {
    openBankPad();
  }
}
function submit() {
	showLoading()
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id,
  };
  toPublish('store/clearTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    closeBankPad();
    if (code === 1) {
      addGroup();
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}
function addGroup() {
  const payload = {
    user_id,
    bao_id: Number(borId.value),
  };
  toPublish('store/addTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    if (code === 1) {
      repeat.value = true;
      successToast(msg);
      navTo('groupBenefits/groupEditor');
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}
function openBankPad() {
  PobulicePopup.value.open();
}
function closeBankPad() {
  PobulicePopup.value.close();
}
</script>

<style scoped lang="less">
@import url('../team/less/groupBao.less');
.fillings {
  image {
    width: 350rpx;
    height: 300rpx;
  }

  text {
    font-size: 32rpx;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666;
    margin-top: 50rpx;
  }
}
</style>